package com.yuan.controller.background;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuan.bean.Book;
import com.yuan.service.BookInformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Controller
public class LibraryManageController {

    @Autowired
    BookInformService bookInformService;

    //用户信息界面
    @GetMapping("/userInformation")
    public String userInformationPage(){
        return "background/userInformation";
    }

    //返回添加书籍页面
    @GetMapping("/bookAdd")
    public String returnBookAddPage(){
        return "background/bookAdd";
    }

    //返回编辑书籍页面
    @GetMapping("/editbooks")
    public String reutrnEditbooks(){
        return "background/editbooks";
    }

    //返回用户借书历史记录
    @GetMapping("/history")
    public String historyPage(){
        return "background/user-returnbooks";
    }

    /**
     * 查询图书数据
     * @param category 图书类型
     * @param bookName 图书名字
     * @param author   图书作者
     * @param pageNum  页码
     * @return         符合条件的图书数据
     */
    @ResponseBody
    @GetMapping("/conditionFind")
    public PageInfo<Book> findBook(@RequestParam(defaultValue = "",value = "category")String category,
                               @RequestParam(defaultValue = "",value = "bookName")String bookName,
                               @RequestParam(defaultValue = "",value = "author")String author,
                               @RequestParam(defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,5);
        final List<Book> books = bookInformService.selectAllByCondition(category, bookName, author);
        PageInfo<Book> pageInfo=new PageInfo<>(books);
        return pageInfo;
    }


    /**
     * 更新书本信息
     * @param book
     * @return
     */
    @ResponseBody
    @PostMapping("/updateBookInfo")
    public boolean updateBookInfo(@RequestBody Book book){
        final boolean b = bookInformService.updateBookInfo(book);
        return b;
    }


    /**
     * 添加书籍
     * @param book 获取前端返回的书籍信息对象
     * @return 返回确认修改是否成功
     */
    @ResponseBody
    @PostMapping("/insertBook")
    public boolean insertBook(@RequestBody Book book){
        final boolean b = bookInformService.insertBook(book);
        return b;
    }

    //通过bookId删除书籍
    @ResponseBody
    @RequestMapping("/del")
    public boolean delBook(@RequestBody Map<String,Integer> map){
        final boolean b = bookInformService.delByBookId(map.get("bookId"));
        return b;
    }


    //通过bookId查找书籍
    @ResponseBody
    @GetMapping("/bookInfo")
    public Book selectByBookId(@RequestParam("bookId")Integer bookId){
        final Book book = bookInformService.selectByBookId(bookId);
        return book;
    }




}
